package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import play.db.jpa.Model;
import utils.MyDateUtils;
import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;

@Entity
public class ProjectVisualizeReport extends Model {
	
	@ManyToOne
	public ProjectVisualize projectVisuzlize;
	
	@ManyToOne
	public ConstructionProject consProject;
	
	public double quanlity;//数量
	
	public double accumulateComp;//累计完成
	
	public int notAllowModify=0;//不允许修改，0可修改，1不可修改
	
	@Transient
	public String pvDescribe;
	
	@Transient
	public long pvId;
	
	@Transient
	public String consProjectName;
	
	@Transient
	public Long consProjectId;
	
	@Transient
	public String consProjectDate;
	
	
	@Transient
	public String pvUnitQuantity;
	
	public String getConsProjectName() {
		if(consProject==null){
			return null;
		}else{
			return consProject.projectName;
		}
	}
	public Long getConsProjectId() {
		if(consProject==null){
			return null;
		}else{
			return consProject.id;
		}
	}
	public String getConsProjectDate(){
		if(consProject==null){
			return null;
		}else{
			return consProject.belongDate;
		}
	}

	public String getPvDescribe(){
		if(projectVisuzlize==null){
			return "";
		}
		return projectVisuzlize.descriptions;
	}
	public String getPvUnitQuantity(){
		if(projectVisuzlize==null){
			return "" ;
		}
		return projectVisuzlize.unitQuantity;
	}
	public long getPvId(){
		if(projectVisuzlize==null){
			return 0;
		}
		return projectVisuzlize.id;
	}

	
	public static String toJSON(List<ProjectVisualizeReport> pvReports){
		return new JSONSerializer()
		.include("id", "quanlity","accumulateComp", "pvDescribe", "pvUnitQuantity", "remark","pvId",
				"consProjectId","consProjectName","consProjectDate")
		.exclude("*").serialize(pvReports);
	}
	
	/**
	 * ConstructionProject operate use for return ProjectVisualizeReport info
	 * @param pvReports 
	 * @return
	 */
	public static String toSimpleJson(List<ProjectVisualizeReport> pvReports){
		String jsonStr="";
		//[{"pvId":1,"quanlity":2,"accumulateComp":3}]
		for(ProjectVisualizeReport pvr: pvReports){
			jsonStr+="{\"pvId\":"+pvr.getPvId()+",\"quanlisty\":"+pvr.quanlity+
					",\"accumulateComp\":"+pvr.accumulateComp+"},";
		}
		if(!"".equals(jsonStr)){
			jsonStr = jsonStr.replaceAll(",$","");
			jsonStr = "["+jsonStr+"]";
		}
		return jsonStr;
	}
	
	public static List<ProjectVisualizeReport> search(Date searchDate,Dept dept){
		List<ProjectVisualizeReport> pvReports = new ArrayList<ProjectVisualizeReport>();
		if(searchDate==null){
			pvReports = ProjectVisualizeReport.find("byBelongDept",dept).fetch();
		}else{
			Date startDate = MyDateUtils.getFirstDayOfMonth(searchDate);
			Date endDate = MyDateUtils.getLastDayOfMonth(searchDate);
			pvReports = ProjectVisualizeReport.find("belongDate <= ? and belongDate >=? and belongDept=?",
					endDate,startDate,dept).fetch();
		}
		return pvReports;
	}
	
	public static void delByConsPro(ConstructionProject consProject){
		ProjectVisualizeReport.delete("consProject=?", consProject);
	}
	
	/**
	 * 更新 notAllowModify 值
	 * @param pvrs
	 * @param notAllowModify 0允许修改，1 不允许修改
	 */
	public static void updAllowModify(List<ProjectVisualizeReport> pvrs,int notAllowModify){
		for(ProjectVisualizeReport pvr:pvrs){
			pvr.notAllowModify=notAllowModify;
			pvr.save();
		}
	}
	

}
